//
//  ASHomeMainCell.m
//  AvatarShow
//
//  Created by xunruiIOS on 2021/4/27.
//

#import "ASHomeMainCell.h"

@implementation ASHomeMainCell

- (instancetype)initWithFrame:(CGRect)frame{
    self = [super initWithFrame:frame];
    if (self) {
        [self addSubview:self.mainImage];
        [self.mainImage mas_makeConstraints:^(MASConstraintMaker *make) {
            make.edges.mas_equalTo(UIEdgeInsetsMake(0, 0, 0, 0));
        }];
    }
    return self;
}

- (UIImageView *)mainImage{
    if (!_mainImage) {
        _mainImage = [UIImageView new];
        _mainImage.layer.masksToBounds = YES;
        _mainImage.layer.cornerRadius = 8;
        _mainImage.contentMode = UIViewContentModeScaleAspectFill;
        _mainImage.clipsToBounds = YES;
        [_mainImage setBackgroundColor:[ASTool backgroundRandomColor]];
    }
    return _mainImage;
}

- (void)setModel:(ASHomeMainModel *)model{
    _model = model;
    [self.mainImage sd_setImageWithURL:[NSURL URLWithString:model.thumb]];
}

- (void)setPhotoModel:(ASPhotoModel *)photoModel{
    _photoModel = photoModel;
    if (photoModel.showImage) {
        [self.mainImage setImage:photoModel.showImage];
    }else{
        [[PHImageManager defaultManager]requestImageDataForAsset:photoModel.asset options:nil resultHandler:^(NSData * _Nullable imageData, NSString * _Nullable dataUTI, UIImageOrientation orientation, NSDictionary * _Nullable info) {
            UIImage * image = [UIImage imageWithData:imageData];
            photoModel.showImage = image;
            [self.mainImage setImage:photoModel.showImage];
        }];
    }
}

@end
